<?php
function jxcompa_query($filepath)
{
   function notice_delp($arraynotice){
      $notice_content = '';
      foreach ($arraynotice as $line) {
         $notice_content .= $line[0] . "||" . $line[1] . "\n";
     }
      return $notice_content;
   }
    $text = file_get_contents($filepath);
    if($text == null || $text == '') return [];
    $lines = explode("\n", $text);
    $parsedCommands = [];
    foreach ($lines as $line) {
        $line = trim($line);
        if (substr($line, -1) === ';') {
            $line = substr($line, 0, -1);
            if (preg_match('/^(\w+)\->(.+)$/', $line, $matches)) {
                $parsedCommands[$matches[1]] = $matches[2];
            } else {
                $parsedCommands['error'] = 'The configuration file is abnormal';
            }
        } else {
            $parsedCommands['error'] = 'The configuration file is abnormal';
        }
    }
    if (isset($parsedCommands['notice'])) {
        $parsedCommands['notice'] = notice_delp(json_decode($parsedCommands['notice'],true));
    }
    return $parsedCommands;
}
function select_sql_tree_folders($conn, $froms_value)
{
    $sql = "SELECT names,id,dicfrom FROM dic WHERE froms = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("s", $froms_value);
    if(!$stmt->execute()){
        return [];
    }
    $result = $stmt->get_result();
    $all_folders = [];
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $all_folders[] = $row;
        }
    }
    function buildTree($folders, $parent = '/') {
      $tree = [];
      foreach ($folders as $folder) {
          if ($folder['dicfrom'] == $parent) {
              $children = buildTree($folders, $folder['id']);
              $node = [
                  'title' => $folder['names'],
                  'id' => $folder['id']
              ];
              if ($children) {
                  $node['children'] = $children;
              }
              $tree[] = $node;
          }
      }
      return $tree;
  }
    $nested_folders = buildTree($all_folders);
    $json_data = json_encode($nested_folders);
    $stmt->close();
    return $json_data;
}

function getwebsiteurls() {
   $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
   $host = $_SERVER['HTTP_HOST'];
   $websiteUrl = $protocol . '://' . $host . '/';
   return $websiteUrl;
}
if(!file_exists($_SERVER['DOCUMENT_ROOT'].'/install.lock')){
    header('Location: /install/');
    exit();
}
$file_getjs_con = file_get_contents('ast/clouddex.js');
require_once $_SERVER['DOCUMENT_ROOT'] . '/upload/token/index.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/cofd/functions.php';
session_start();
$gobiu_oop = false;
if (isset($_SESSION['authorized']) && $_SESSION['authorized']) {
   if (time() < $_SESSION['expires_at']) {
      session_write_close();
   } else {
      $gobiu_oop = true;
      echo '<script>document.addEventListener("DOMContentLoaded", function() {Qmsg.error("The sign-in status has expired");});</script>';
      echo '<script>sessionStorage.removeItem("user");</script>';
   }
} else {
   $gobiu_oop = true;
}
$session_token = isset($_SESSION['token']) ? $_SESSION['token'] : null;
if ($session_token == null || $session_token == "") {
   $_SESSION['token']  = nowgettoken();
} else {
   if (!checknowtokentime($_SESSION['token'])) {
      $_SESSION['token'] = nowgettoken();
   }
}
$session_token = $_SESSION['token'];
$session_token_enc = encrypt($session_token, "E", "yhsdylpo9342");
$useridfromphp = $_COOKIE['user_key'] ?? '';
require($_SERVER['DOCUMENT_ROOT'] . '/cofd/common.php');
if ($useridfromphp == '') {
} else {
   $useridfromphp_enc = encrypt($useridfromphp, "E", "yhsdylpo9342");
   $websiteurlj_space = getwebsiteurls().'share/'.$useridfromphp_enc;
   if(!file_exists($_SERVER['DOCUMENT_ROOT'].'/uploads/'.$useridfromphp.'/')){
      mkdir($_SERVER['DOCUMENT_ROOT'].'/uploads/'.$useridfromphp.'/');
   }
   if(!file_exists($_SERVER['DOCUMENT_ROOT'].'/uploads/'.$useridfromphp.'/drivenormalpz.afikconfig')){
      $file = fopen($_SERVER['DOCUMENT_ROOT'].'/uploads/'.$useridfromphp.'/drivenormalpz.afikconfig', 'w') or die('Unable to create file');
      fclose($file);
   }else{
      $jxcompa_query_result = jxcompa_query($_SERVER['DOCUMENT_ROOT'].'/uploads/'.$useridfromphp.'/drivenormalpz.afikconfig');
   }
   $sql = "SELECT capacity, capacityed,rate,rated,vip FROM user WHERE id = ?";
   $stmt = $conn->prepare($sql);
   $stmt->bind_param("s", $useridfromphp);
   if ($stmt->execute()) {
      $result = $stmt->get_result();
      if ($row = $result->fetch_assoc()) {
         if (isset($row['capacity']) && isset($row['capacityed'])) {
            if ($row['capacity'] > 0) {
               $nowsizeincapacity = round($row['capacityed'] / $row['capacity'] * 100, 2);
            } else {
               $nowsizeincapacity = 0; 
            }
         } else {
            $nowsizeincapacity = 0; 
         }
         if (isset($row['rate']) && isset($row['rated'])) {
            if ($row['rate'] > 0) {
               $nowsizeinrate = round($row['rated'] / $row['rate'] * 100, 2);
            } else {
               $nowsizeinrate = 0; 
            }
         } else {
            $nowsizeinrate = 0; 
         }
      } else {
         $nowsizeincapacity = 0; 
         $nowsizeinrate = 0; 
      }
   } else {
      $nowsizeincapacity = 0; 
      $nowsizeinrate = 0; 
   }
   $vip_time = $row['vip'] ?? '1970-01-01 00:00:00';
   $currentDateTime_compavip = new DateTime(); 
   $vipnowtime = new DateTime($vip_time);
   if($vipnowtime > $currentDateTime_compavip){
      $vip_now_status_ok = true;
   }else{
      $vip_now_status_ok = false;
   }
   $array_setting_treefolder = select_sql_tree_folders($conn, $useridfromphp);
}